package com.zhu.SupermarketInventoryManagement.controller;


import com.zhu.SupermarketInventoryManagement.model.DO.AdminDO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/goods")
public class GoodsListController {

    @GetMapping("/home")
    public ModelAndView home(HttpSession session) {
        ModelAndView mav = new ModelAndView();
        AdminDO admin = (AdminDO) session.getAttribute("admin");
        if (admin != null) {
            mav.addObject("admin", admin);
        } else {
            mav.addObject("admin", new AdminDO());
        }
        mav.setViewName("/admin/goods_list");
        return mav;
    }

    @GetMapping("/list")
    public ModelAndView list() {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("/admin/goods_list");
        return mav;
    }

    @GetMapping("/detail/{name}/{price}/{cost}/{number}/{categoryId}")
    public ModelAndView detail(@PathVariable String name, @PathVariable String price, @PathVariable String cost, @PathVariable String number, @PathVariable String categoryId) {
        ModelAndView mav = new ModelAndView();
        mav.addObject("name", name);
        mav.addObject("price", price);
        mav.addObject("cost", cost);
        mav.addObject("number", number);
        mav.addObject("categoryId", categoryId);
        mav.setViewName("/admin/detail");
        return mav;
    }

    @GetMapping("/record/{name}/{price}/{cost}/{number}/{categoryId}/{adminId}")
    public ModelAndView record(@PathVariable String name, @PathVariable String price, @PathVariable String cost, @PathVariable String number, @PathVariable String categoryId, @PathVariable String adminId) {
        ModelAndView mav = new ModelAndView();
        mav.addObject("name", name);
        mav.addObject("price", price);
        mav.addObject("cost", cost);
        mav.addObject("number", number);
        mav.addObject("categoryId", categoryId);
        mav.addObject("adminId", adminId);
        mav.setViewName("/admin/record");
        return mav;
    }

    @GetMapping("/update/{name}/{price}/{cost}/{number}/{categoryId}/{id}")
    public ModelAndView update(@PathVariable String name, @PathVariable String price, @PathVariable String cost, @PathVariable String number, @PathVariable String categoryId, @PathVariable String id) {
        ModelAndView mav = new ModelAndView();
        mav.addObject("name", name);
        mav.addObject("price", price);
        mav.addObject("cost", cost);
        mav.addObject("number", number);
        mav.addObject("categoryId", categoryId);
        mav.addObject("id", id);
        mav.setViewName("/admin/update");
//        System.out.println("GoodsListController 数据: "+mav);
        return mav;
    }


    @GetMapping("/add")
    public ModelAndView add() {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("/admin/add");
        return mav;
    }
}
